/*
 * Copyright (C) 2017 Qi Cai Technology Co., Ltd. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package com.example.lkslib.dbmanager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO this class desription here
 * <p>
 * Created by ww on 2017/7/19.
 */
public  class LksSqliteOpenHelper extends OrmLiteSqliteOpenHelper {
    private static final String TABLE_NAME = "wubopda.db";
    private static final int VERSION=7;
    private static LksSqliteOpenHelper instance;
    private Map<String,OrmDaoUtils> helperMap = new HashMap<>();
    public LksSqliteOpenHelper(Context context) {
        super(context, TABLE_NAME, null, VERSION);
    }
    /**
     * 单例获取该Helper
     *
     * @param context
     * @return
     */
    public static synchronized LksSqliteOpenHelper getHelper(Context context) {
        if (instance == null) {
            synchronized (LksSqliteOpenHelper.class) {
                if (instance == null)
                    instance = new LksSqliteOpenHelper(context);
            }
        }
        return instance;
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        List<Class> classList = DBInitUtil.getClassList();
        if(null!=classList&&classList.size()>0){
            for (Class aClass : classList) {
                try {
                    TableUtils.createTable(connectionSource,aClass);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource,
                          int i, int i1) {
        try{
            List<UpDateHelperBean> beanList = DBInitUtil.getBeanList();
            if(null!=beanList&&beanList.size()>0){
                for (UpDateHelperBean upDateHelperBean : beanList) {
                    if(null==upDateHelperBean)
                        continue;
                    if(i<upDateHelperBean.getVersion()){
                        List<String> strings=upDateHelperBean.getUpDateStrings();
                        for (String s:strings ) {
                            getDao(upDateHelperBean.getaClass()).executeRaw(s);

                        }
                    }
                }
            }
        }catch (Exception e){

        }

    }
    public <T> OrmDaoUtils<T> getCustomDao(Context context, Class<T> aClass) throws SQLException {
        String className = aClass.getSimpleName();
        if(!helperMap.containsKey(className)){
            helperMap.put(className,new DaoImp(context,aClass));
        }
        return helperMap.get(className);
    }

    @Override
    public void close() {
        super.close();
        helperMap.clear();
    }

}
